const express = require('express');
const path = require('path');
const cors = require('cors');

const app = express();
const port = process.env.PORT || 8001;

// 使用 cors 中间件
app.use(cors());

const fs = require('fs');
const jsFilePath = path.join(__dirname, 'dist', 'assets', 'web1MicroFrontendEntry.js');
if (fs.existsSync(jsFilePath)) {
    console.log('web1MicroFrontendEntry.js exists');
} else {
    console.log('web1MicroFrontendEntry.js does not exist');
}

app.use((req, res, next) => {
  const originalSend = res.send;
  res.send = function (body) {
      console.log('Response Headers:', res.getHeaders());
      originalSend.call(this, body);
  };
  next();
});



// 静态文件服务，将 dist 目录作为静态资源目录
// app.use(express.static(path.join(__dirname, 'dist')));

// 静态文件服务，将 dist 目录作为静态资源目录
app.use(express.static(path.join(__dirname, 'dist'), {
  setHeaders: (res, filePath) => {
    if (path.extname(filePath) === '.js') {
      res.setHeader('Content-Type', 'application/javascript');
    }
  }
}));




// 处理所有路由，返回 index.html 文件
app.get('/index.html', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});

// 启动服务器
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});